package org.csu.mypetstore.api.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import org.csu.mypetstore.api.Vo.AccountVO;
import org.csu.mypetstore.api.common.CommonResponse;
import org.csu.mypetstore.api.entity.Account;
import org.csu.mypetstore.api.entity.BannerData;
import org.csu.mypetstore.api.entity.Profile;
import org.csu.mypetstore.api.entity.SignOn;
import org.csu.mypetstore.api.persistence.AccountMapper;
import org.csu.mypetstore.api.persistence.BannerDataMapper;
import org.csu.mypetstore.api.persistence.ProfileMapper;
import org.csu.mypetstore.api.persistence.SignOnMapper;
import org.csu.mypetstore.api.service.AccountService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service("accountService")
public class AccountServiceImpl implements AccountService {

    @Autowired
    private AccountMapper accountMapper;
    @Autowired
    private BannerDataMapper bannerDataMapper;
    @Autowired
    private ProfileMapper profileMapper;
    @Autowired
    private SignOnMapper signOnMapper;

    @Override
    public CommonResponse<AccountVO> getAccount(String username, String password) {
        QueryWrapper<SignOn> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("username",username);
        queryWrapper.eq("password",password);
        SignOn signOn = signOnMapper.selectOne(queryWrapper);
        if(signOn == null)
        {
            return CommonResponse.createForError("用户名密码不正确");
        }
        return getAccount(username);
    }

    @Override
    public CommonResponse<AccountVO> insertAccount(AccountVO accountVO) {
        Account account1 = accountMapper.selectById(accountVO.getUsername());
        //注册时用户名已存在
        if (account1 != null){
            return CommonResponse.createForSuccessMessage("该用户已存在");
        }
        Account account = new Account();
        account.setUsername(accountVO.getUsername());
        account.setEmail(accountVO.getEmail());
        account.setFirstName(accountVO.getFirstName());
        account.setLastName(accountVO.getLastName());
        account.setStatus(accountVO.getStatus());
        account.setAddress1(accountVO.getAddress1());
        account.setAddress2(accountVO.getAddress2());
        account.setCity(accountVO.getCity());
        account.setState(accountVO.getState());
        account.setZip(accountVO.getZip());
        account.setCountry(accountVO.getCountry());
        account.setPhone(accountVO.getPhone());

        Profile profile = new Profile();
        profile.setUsername(accountVO.getUsername());
        profile.setLanguagePreference(accountVO.getLanguagePreference());
        profile.setFavouriteCategoryId(accountVO.getFavouriteCategoryId());
        profile.setListOption(accountVO.getListOption());
        profile.setBannerOption(accountVO.getBannerOption());

        SignOn signOn = new SignOn();
        signOn.setUsername(accountVO.getUsername());
        signOn.setPassword(accountVO.getPassword());

        accountMapper.insert(account);
        profileMapper.insert(profile);
        signOnMapper.insert(signOn);
        return CommonResponse.createForSuccess("注册成功",accountVO);
    }

    @Override
    public CommonResponse<AccountVO> updateAccount(String username, AccountVO accountVO) {
        Account account = new Account();
        account.setUsername(accountVO.getUsername());
        account.setEmail(accountVO.getEmail());
        account.setFirstName(accountVO.getFirstName());
        account.setLastName(accountVO.getLastName());
        account.setStatus(accountVO.getStatus());
        account.setAddress1(accountVO.getAddress1());
        account.setAddress2(accountVO.getAddress2());
        account.setCity(accountVO.getCity());
        account.setState(accountVO.getState());
        account.setZip(accountVO.getZip());
        account.setCountry(accountVO.getCountry());
        account.setPhone(accountVO.getPhone());

        Profile profile = new Profile();
        profile.setUsername(accountVO.getUsername());
        profile.setLanguagePreference(accountVO.getLanguagePreference());
        profile.setFavouriteCategoryId(accountVO.getFavouriteCategoryId());
        profile.setListOption(accountVO.getListOption());
        profile.setBannerOption(accountVO.getBannerOption());


        SignOn signOn = new SignOn();
        signOn.setUsername(accountVO.getUsername());
        signOn.setPassword(accountVO.getPassword());

        UpdateWrapper<Account> updateWrapper = new UpdateWrapper<>();
        updateWrapper.eq("userid",username);
        accountMapper.update(account,updateWrapper);
        profileMapper.updateById(profile);
        signOnMapper.updateById(signOn);
        return CommonResponse.createForSuccess("更新成功",accountVO);
    }

    @Override
    public CommonResponse<AccountVO> getAccount(String username) {
        Account account = accountMapper.selectById(username);
        Profile profile = profileMapper.selectById(username);
        BannerData bannerData = bannerDataMapper.selectById(profile.getFavouriteCategoryId());
        if(account == null)
        {
            return CommonResponse.createForError("获取用户信息失败");
        }

        AccountVO accountVO  = entityToVO(account,profile);

        return CommonResponse.createForSuccess(accountVO);
    }

    private AccountVO entityToVO(Account account,Profile profile) {
        AccountVO accountVO = new AccountVO();

        accountVO.setUsername(account.getUsername());
        accountVO.setFirstName(account.getFirstName());
        accountVO.setLastName(account.getLastName());
        accountVO.setEmail(account.getEmail());
        accountVO.setStatus(account.getStatus());
        accountVO.setState(account.getState());
        accountVO.setAddress1(account.getAddress1());
        accountVO.setAddress2(account.getAddress2());
        accountVO.setCity(account.getCity());
        accountVO.setZip(account.getZip());
        accountVO.setPhone(account.getPhone());
        accountVO.setCountry(account.getCountry());

        accountVO.setLanguagePreference(profile.getLanguagePreference());
        accountVO.setFavouriteCategoryId(profile.getFavouriteCategoryId());
        accountVO.setListOption(profile.getListOption());
        accountVO.setBannerOption(profile.getBannerOption());

        SignOn SignOn = signOnMapper.selectById(account.getUsername());
        accountVO.setPassword(SignOn.getPassword());

        return accountVO;
    }

//    private AccountVO entityToVO(Account account,Profile profile,BannerData bannerData)
//    {
//
//        AccountVO accountVO = new AccountVO();
//        accountVO.setUsername(account.getUsername());
//        accountVO.setPassword("");
//        accountVO.setEmail(account.getEmail());
//        accountVO.setFirstName(account.getFirstName());
//        accountVO.setLastName(account.getLastName());
//        accountVO.setStatus(account.getStatus());
//        accountVO.setAddress1(account.getAddress1());
//        accountVO.setAddress2(account.getAddress2());
//        accountVO.setCity(account.getCity());
//        accountVO.setState(account.getState());
//        accountVO.setCountry(account.getCountry());
//        accountVO.setZip(account.getZip());
//        accountVO.setPhone(account.getPhone());
//
//        accountVO.setLanguagePreference(profile.getLanguagePreference());
//        accountVO.setBannerOption(profile.getBannerOption() == 1);
//        accountVO.setListOption(profile.getListOption() == 1);
//        if (profile.getBannerOption() == 1) {
//            accountVO.setFavouriteCategoryId(profile.getFavouriteCategoryId());
//            accountVO.setBannerName(bannerData.getBannerName());
//        }else{
//            accountVO.setFavouriteCategoryId("");
//            accountVO.setBannerName("");
//        }
//        return accountVO;
//    }
}
